package com.cqxyd.smartsite.controller.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqxyd.smartsite.pojo.AdminInfo;
import com.cqxyd.smartsite.service.LoginService;
import com.cqxyd.smartsite.util.MD5Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @ClassName: HospitalInfoController
 * @Description: 用户登录控制层
 * @author Angus
 * @date 2018-11-21
 */
@Controller
@ApiIgnore
@RequestMapping(value = "/password")
public class passwordController {
	@Autowired
	private LoginService loginService;

	@RequestMapping(value = "/changePassword")
	public String changePassword() {
		return "admin-password-edit";
	}

	@RequestMapping(value = "/saveNewPassword")
	@ResponseBody
	public String saveNewPassword(String oldpassword, String newpassword, HttpServletRequest request,
			HttpServletResponse response) {
		AdminInfo adminInfo = (AdminInfo) (request.getSession().getAttribute("adminInfo"));
		AdminInfo adminInfoDb = loginService.getAdminInfo(adminInfo.getAdminId());
		// 密码MD5加密
		String md5Password = MD5Helper.MD5Encrypt(oldpassword);
		if (md5Password.equals(adminInfoDb.getAdminPassword())) {

			adminInfoDb.setAdminPassword(MD5Helper.MD5Encrypt(newpassword));
			int result = loginService.updateAdminInfo(adminInfoDb);
			if (result == 0) {
				return "{\"success\":false,\"error\":\"保存密码失败\"}";
			}
		} else {
			return "{\"success\":false,\"error\":\"旧密码错误\"}";
		}

		return "{\"success\":true}";
	}

}
